package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.aliyun.OSSService;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.WeMediaThreadLocal;
import com.heima.wemedia.dto.WmMaterialDto;
import com.heima.wemedia.entity.WmMaterial;
import com.heima.wemedia.entity.WmNewsMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.wemedia.service.IWmNewsMaterialService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2021-05-20
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private OSSService ossService;

    @Autowired
    private IWmNewsMaterialService newsMaterialService;

    @Override
    public ResponseResult upload(MultipartFile file){
        try {
            String url = ossService.upload(file);
            WmMaterial wmMaterial = new WmMaterial();
            User user = WeMediaThreadLocal.get();
            if (user==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }

            wmMaterial.setUserId(user.getUserId());
            wmMaterial.setUrl(url);
            wmMaterial.setType(0);
            wmMaterial.setIsCollection(false);
            wmMaterial.setCreatedTime(new Date());
            this.save(wmMaterial);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult findListByCollection(WmMaterialDto dto) {
        //获取用户
        User user = WeMediaThreadLocal.get();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //判断参数
        LambdaQueryWrapper<WmMaterial> query = new LambdaQueryWrapper<>();
        query.eq(WmMaterial::getUserId,user.getUserId());
        if (dto.getIsCollection()!=null){
            query.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        query.orderByDesc(WmMaterial::getCreatedTime);


        IPage<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        IPage<WmMaterial> iPage = this.page(page, query);
        return new PageResponseResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public ResponseResult deletePicture(Integer id) {
        //获取用户
        User user = WeMediaThreadLocal.get();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        WmMaterial wmMaterial = this.getById(id);
        if (wmMaterial == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<WmNewsMaterial> query = new LambdaQueryWrapper<>();
        query.eq(WmNewsMaterial::getMaterialId,id);
        int count = newsMaterialService.count(query);
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_CAN_NOT_DELETE);
        }
        this.removeById(id);
        ossService.deleteFile(wmMaterial.getUrl());
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult isCollect(Integer id, int i) {
        //获取用户
        User user = WeMediaThreadLocal.get();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        LambdaUpdateWrapper<WmMaterial> update = new LambdaUpdateWrapper<>();
        update.eq(WmMaterial::getId,id).eq(WmMaterial::getUserId,user.getUserId());
        update.set(WmMaterial::getIsCollection,i);
        this.update(update);
        return ResponseResult.okResult();
    }
}
